package com.hibikiluler.javalearning.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.hibikiluler.javalearning.entity.SysAdmins;
import com.hibikiluler.javalearning.entity.SysMenus;
import com.hibikiluler.javalearning.entity.SysRole;
import com.hibikiluler.javalearning.mapper.SysAdminMapper;
import com.hibikiluler.javalearning.mapper.SysMenusMapper;
import com.hibikiluler.javalearning.mapper.SysRoleMapper;
import com.hibikiluler.javalearning.service.SysMenusService;
import com.hibikiluler.javalearning.service.SysRoleService;
import com.hibikiluler.javalearning.util.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j

public class SysRoleServiceImpl implements SysRoleService {

    @Autowired
    private SysRoleMapper sysRoleMapper;

    @Autowired
    private RedisUtils redisUtils;

    @Autowired
    private SysAdminMapper sysAdminMapper;

    @Autowired
    private SysMenusMapper sysMenusMapper;

    @Transactional
    @Override
    public Result insert(SysRole sysRole) {
        //查询角色信息是否存在
        SysRole role = sysRoleMapper.findByLabel(sysRole.getLabel());
        if (null != role) {
            return Result.fail("该角色已经存在");
        }
        //插入角色信息
        sysRoleMapper.insert(sysRole);
        if (sysRole.getAdminMap().size() > 0) {
            sysRole.getAdminMap().forEach(item -> {
                log.info("添加权限数据");
                sysRoleMapper.insertAdmin(sysRole.getId(),item.getId());
            });

        }
        if (sysRole.getMenuMap().size() > 0) {
            sysRole.getMenuMap().forEach(item-> {
                log.info("添加菜单数据");
                sysRoleMapper.insertMenus(sysRole.getId(),item.getId());
            });
        }
        redisUtils.delKey("userInfo_" + SecurityUtils.getUsername());
        return Result.success("添加角色数据成功");
    }


    @Override
    public Result delete(Long id) {
        log.info("查询该角色信息下是否有菜单权限");
        List<SysMenus> menus = sysMenusMapper.findByRoleId(id);
        List<SysMenus> children = new ArrayList<>();
        menus.forEach(item -> {
            children.addAll(sysMenusMapper.findByRoleIdAndParentId(item.getId(),id));
        });
        if (menus.size() > 0 || children.size() > 0) {
            return Result.fail("删除失败，该角色含有菜单信息，请先删除相应的菜单信息");
        }
        if (sysAdminMapper.findByRoleId(id).size() > 0) {
            return Result.fail("删除失败，该角色含有权限信息，请先删除相应的权限信息");
        }

        sysRoleMapper.delete(id);
        redisUtils.delKey("userInfo_" + SecurityUtils.getUsername());
        return Result.success("删除角色数据成功");
    }

    @Transactional
    @Override
    public Result update(SysRole sysRole) {
        sysRoleMapper.update(sysRole);
        if (sysRole.getAdminMap().size() > 0) {
            log.info("先删除权限数据");
            sysRoleMapper.deleteAdminByRoleId(sysRole.getId());
            sysRole.getAdminMap().forEach(item -> {
                log.info("添加权限数据");
                sysRoleMapper.insertAdmin(sysRole.getId(),item.getId());
            });

        }
        if (sysRole.getMenuMap().size() > 0) {
            log.info("先删除菜单数据");
            sysRoleMapper.deleteMenusByRoleId(sysRole.getId());
            sysRole.getMenuMap().forEach(item-> {
                log.info("添加菜单数据");
                sysRoleMapper.insertMenus(sysRole.getId(),item.getId());
            });
        }
        redisUtils.delKey("userInfo_" + SecurityUtils.getUsername());
        return Result.success("修改角色数据成功");
    }

    @Override
    public Result findPage(Queryinfo queryinfo) {
        log.info("开始权限数据分页-->页吗{},-->页数{}-->查询内容{},",queryinfo.getPagenumber(),queryinfo.getPagesize(),queryinfo.getQueryString());
        //第一个参数为页码 第二个为页数大小
        PageHelper.startPage(queryinfo.getPagenumber(), queryinfo.getPagesize());
        Page<SysRole> page = sysRoleMapper.findPage(queryinfo.getQueryString());
        long total = page.getTotal();
        List<SysRole> result = page.getResult();
        result.forEach(item -> {
            //查询角色下的菜单信息
            List<SysMenus> menus = sysMenusMapper.findByRoleId(item.getId());
            menus.forEach(menu -> {
                List<SysMenus> children = sysMenusMapper.findByRoleIdAndParentId(menu.getId(),item.getId());
                menu.setChildren(children);
            });
            item.setMenuMap(menus);
            //查询角色下的权限信息
            List<SysAdmins> admins = sysAdminMapper.findByRoleId(item.getId());
            item.setAdminMap(admins);
        });
        log.info("查询的总条数-->{}",total);
        log.info("分页列表-->{}",result);
        return PageResult.pageResult(total,result);
    }

    @Override
    public Result findAll() {
        return Result.success("查询所有角色成功", sysRoleMapper.findAll());
    }
}
